package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.domain.Department;
import com.ruoyi.system.domain.Employee;
import com.ruoyi.system.service.IDepartmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Minister;
import com.ruoyi.system.service.IMinisterService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 部长信息Controller
 * 
 * @author ruoyi
 * @date 2025-07-01
 */
@Controller
@RequestMapping("/system/minister")
public class MinisterController extends BaseController
{
    private String prefix = "system/minister";

    @Autowired
    private IMinisterService ministerService;
    @Autowired
    private IDepartmentService departmentService;

    @RequiresPermissions("system:minister:view")
    @GetMapping()
    public String minister()
    {
        return prefix + "/minister";
    }

    /**
     * 查询部长信息列表
     */
    @RequiresPermissions("system:minister:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Minister minister)
    {
        startPage();
        List<Minister> list = ministerService.selectMinisterList(minister);
     /*   for (Employee employee1 : list) {
            Long departmentId = employee1.getDepartmentId();
            //根据部门的编号查询部门
            Department department = departmentService.selectDepartmentByDepartmentId(departmentId);
            if(department != null) {
                String departmentName = department.getDepartmentName();
                employee1.setDepartmentName(departmentName);
            }
        }*/

        for (Minister minister1 : list) {
            Long departmentId = minister1.getDepartmentId();
            //根据部门的编号查询部门
            Department department = departmentService.selectDepartmentByDepartmentId(departmentId);
            if(department != null) {
                String departmentName = department.getDepartmentName();
                minister1.setDepartmentName(departmentName);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出部长信息列表
     */
    @RequiresPermissions("system:minister:export")
    @Log(title = "部长信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Minister minister)
    {
        List<Minister> list = ministerService.selectMinisterList(minister);
        ExcelUtil<Minister> util = new ExcelUtil<Minister>(Minister.class);
        return util.exportExcel(list, "部长信息数据");
    }

    /**
     * 新增部长信息
     */
    @RequiresPermissions("system:minister:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存部长信息
     */
    @RequiresPermissions("system:minister:add")
    @Log(title = "部长信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Minister minister)
    {
        return toAjax(ministerService.insertMinister(minister));
    }

    /**
     * 修改部长信息
     */
    @RequiresPermissions("system:minister:edit")
    @GetMapping("/edit/{ministerId}")
    public String edit(@PathVariable("ministerId") Long ministerId, ModelMap mmap)
    {
        Minister minister = ministerService.selectMinisterByMinisterId(ministerId);
        mmap.put("minister", minister);
        return prefix + "/edit";
    }

    /**
     * 修改保存部长信息
     */
    @RequiresPermissions("system:minister:edit")
    @Log(title = "部长信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Minister minister)
    {
        return toAjax(ministerService.updateMinister(minister));
    }

    /**
     * 删除部长信息
     */
    @RequiresPermissions("system:minister:remove")
    @Log(title = "部长信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ministerService.deleteMinisterByMinisterIds(ids));
    }
}
